/*
 * @Author: ZhangXingliang
 * @Date: 2022-01-13 15:03:37
 * @Last Modified by: ZhangXingliang
 * @Last Modified time: 2022-05-06 11:37:34
 */

import { Clip } from '@/core/clip'
import { Track } from '@/core/track'
/**
 *
 * 添加一个素材到storyboard末尾
 * @export
 */
export function appendClip(track: Track, meta: any) {
  return insertClip(track, Math.min(0, track.clips.length), meta)
}

/**
 *
 * 插入一个素材到某处
 * @export
 * @param {number} index 插入的位置
 * @param {*} meta 素材的元数据
 */
export function insertClip(track: Track, index: number, meta: any) {
  let baseClip = track.clips[index - 1] || { offset: 0, duration: 0 }
  // 多个素材的情况的offset还未考虑
  const clips = meta.map((i: any, index: number) => {
    let clip = new Clip({
      name: i.entity.name,
      offset: baseClip.offset + baseClip.duration,
      duration: i.entity.item.length / 10000000,
      start: 0,
      src: i.entity.item.clipfile[1].filename,
      ref: {
        url: i.entity.item.clipfile[1].filename,
        length: i.entity.item.length / 10000000,
        in: 0,
        out: i.entity.item.length / 10000000,
      },
      thumbnail: i.entity.iconfilename,
      id: track.clips.length + index,
    })
    baseClip = clip
    return clip
  })
  const oldClips = track.clips.slice(index)
  for (let oldClip of oldClips) {
    oldClip.offset = baseClip.offset + baseClip.duration
    baseClip = oldClip
    clips.push(oldClip)
  }
  return track.splice(index, oldClips.length, ...clips)
}

export function splitClip(track: Track, time: number) {
  const clip = getClipByTime(track, time)
  if (clip) {
    const copiedClip = clip.clone()
    copiedClip.offset = time
    copiedClip.duration = clip.offset + clip.duration - time
    copiedClip.id = `${clip.id}_${+new Date()}`
    clip.duration = time - clip.offset
    if (clip.ref && copiedClip.ref) {
      clip.ref.out = clip.duration + clip.ref.in
      copiedClip.ref.in = clip.ref.out
      copiedClip.ref.out = clip.ref.out + copiedClip.duration
    }
    track.splice(track.clips.indexOf(clip), 1, clip, copiedClip)
    return true
  } else {
    return false
  }
}

/**
 *
 * 替换某处的素材为新素材
 * @export
 * @param {number} index 替换的位置
 * @param {*} meta 素材的元数据
 */
export function replaceClip(track: Track, index: number, meta: any) {
  let baseClip = track.clips[index]
  const clips = meta.map((i: any, index: number) => {
    let clip = new Clip({
      name: i.entity.name,
      offset: baseClip.offset + baseClip.duration,
      duration: i.entity.item.length / 10000000,
      start: 0,
      src: i.entity.item.clipfile[1].filename,
      ref: {
        id: i.entity.guid,
        url: i.entity.item.clipfile[1].filename,
        length: i.entity.item.length / 10000000,
        in: 0,
        out: i.entity.item.length / 10000000,
      },
      thumbnail: i.entity.iconfilename,
      id: track.clips.length + index,
    })
    baseClip = clip
    return clip
  })
  const oldClips = track.clips.slice(index)
  for (let oldClip of oldClips) {
    oldClip.offset = baseClip.offset + baseClip.duration
    baseClip = oldClip
    clips.push(oldClip)
  }
  return track.splice(index, oldClips.length + 1, ...clips)
}

/**
 *
 * 移动素材到某处
 * @export
 * @param {number} from 起始位置
 * @param {number} to 目标位置
 */
export function moveClip(track: Track, from: number, to: number) {
  const clips = track.clips
  const index = from > to ? from + 1 : from
  clips.splice(to, 0, clips[from])
  clips.splice(index, 1)
  let offset = 0
  for (let clip of clips) {
    clip.offset = offset
    offset += clip.duration
  }
  track.splice(0, clips.length, ...clips)
}

export function removeClip(track: Track, clip: Clip) {
  const index = track.clips.findIndex((i) => i.id === clip.id)
  const clips = track.clips
  clips.splice(index, 1)
  let offset = 0
  for (let clip of clips) {
    clip.offset = offset
    offset += clip.duration
  }
  track.splice(0, clips.length, ...clips)
}

export function updateClip(track: Track, clip: Clip) {
  const index = track.clips.findIndex((i) => i.id === clip.id)
  const clips = track.clips
  clips.splice(index, 1, clip)
  let offset = 0
  for (let clip of clips) {
    clip.offset = offset
    offset += clip.duration
  }
  track.splice(0, clips.length, ...clips)
}
export function getClipByTime(track: Track, time: number) {
  if (time > track.duration || time < 0) {
    return null
  }
  for (let clip of track.clips) {
    if (clip.offset <= time && clip.duration + clip.offset >= time) {
      return clip
    }
  }
  return null
}

export function getTimeByPosition(track: Track, pos: number) {
  if (pos <= 0) {
    return 0
  } else if (pos >= getMaxPosition(track)) {
    return track.duration
  }
  const index = Math.floor(pos / 240)
  const offset = pos % 240
  let ct = (offset / 240) * track.clips[index].duration
  for (let i = 0; i < index; i++) {
    ct += track.clips[i].duration
  }
  return ct
}

export function getPositionByTime(track: Track, time: number) {
  if (time <= 0) {
    return 0
  } else if (time >= track.duration) {
    return getMaxPosition(track)
  }
  let pos = 0
  for (let clip of track.clips) {
    if (time > clip.duration) {
      pos += 240
      time -= clip.duration
    } else {
      pos += 240 * (time / clip.duration)
      break
    }
  }
  return pos
}

export function getMaxPosition(track: Track) {
  return track.clips.length * 240
}

export function getUnhandlePosition(track: Track, pos: number) {
  const maxPos = getMaxPosition(track)
  return pos < 0 ? pos : pos > maxPos ? pos - maxPos : 0
}
